package com.xjtu.backend.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xjtu.backend.entity.*;
import com.xjtu.backend.mapper.*;
import com.xjtu.backend.result.Result;

import java.util.*;


/**
 * 完成注册功能，将信息写入到用户表中
 */

@RestController
public class RegisterController {

    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private SoliderMapper soliderMapper;
    @Autowired
    private StudentMapper studentMapper;

    static private long id = 0;


    @PostMapping("/register/org")
    public Result<String> registerOrg(Org org1) {
        List<Org> lst = orgMapper.selectList(new QueryWrapper<Org>().eq("name", org1.getName()));
        if (!lst.isEmpty()) {
            return Result.error("it has been registered");
        }

        // 进行插入
        org1.setId(id++);
        if (orgMapper.insert(org1) == 0) {
            return Result.error();
        }
        return Result.ok();
    }

    @PostMapping("/register/solider")
    public Result<String> registerSolider(Solider solider1) {
        // 查找当前的名称是否被注册
        List<Solider> lst = soliderMapper.selectList(new QueryWrapper<Solider>().eq("name", solider1.getName()));

        // 如果已经被注册，则返回错误
        if (!lst.isEmpty()) {
            return Result.error("it has been registered");
        }

        // 进行插入
        solider1.setId(id++);
        if (soliderMapper.insert(solider1) == 0) {
            return Result.error();  // 插入错误
        }
        return Result.ok();
    }

    @PostMapping("/register/student")
    public Result<String> registerStudent(Student student1) {
        // 查找当前的名称是否被注册
        List<Student> lst = studentMapper.selectList(new QueryWrapper<Student>().eq("name", student1.getName()));
        if (!lst.isEmpty()) {
            return Result.error("it has been registered");
        }

        // 进行插入
        student1.setId(id++);
        if (studentMapper.insert(student1) == 0) {
            return Result.error();
        }
        return Result.ok();
    }
}
